<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="借阅人" prop="borrower">
            <user-select v-model="form.borrower" placeholder="请选择借阅人" :max-show="1"></user-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="借阅单位" prop="borrowingUnit">
            <treeselect v-model="form.borrowingUnit" :options="deptOptions" :show-count="true"
                        placeholder="请选择借阅单位"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="被借阅人" prop="borrowerFrom">
            <person-select v-model="form.borrowerFrom" placeholder="请选择被借阅人"></person-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="借阅开始时间" prop="startDate">
            <el-date-picker clearable size="small" style="width: 100%"
                            v-model="form.startDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择借阅开始时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="借阅结束时间" prop="endDate">
            <el-date-picker clearable size="small" style="width: 100%"
                            v-model="form.endDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择借阅结束时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="档案类型" prop="fileType">
            <el-checkbox-group v-model="selectDocOptions" @change="handleCheckedDocChange">
              <el-checkbox v-for="city in docOptions" :label="city.id" :key="city.name">{{city.name}}</el-checkbox>
            </el-checkbox-group>

          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否打印" prop="whetherPrint">
            <el-radio-group v-model="form.whetherPrint">
              <el-radio :disabled="isView"
                        v-for="dict in whetherPrintOptions"
                        :key="dict.dictValue"
                        :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="false" :span="12">
          <el-form-item label="借阅状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择借阅状态" style="width: 100%;">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="parseInt(dict.dictValue)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  <div v-show="parentTaskId===null" class="dialog-footer" style="text-align:center">
    <el-button :loading="buttonLoading" size="small" type="info" @click="submitCallback">关闭</el-button>
    <el-button :loading="buttonLoading" size="small" type="primary" @click="submitForm">提交</el-button>
  </div>
  <!-- 工作流开始 -->
  <verify ref="verifyRef" :taskId="taskId" @submitCallback="submitCallback"
          :taskVariables="taskVariables" :sendMessage="sendMessage"></verify>
  <!-- 工作流结束 -->
  </div>
</template>
<script setup>
import verify from "@/components/Process/Verify";
import UserSelect from "@/components/UserSelect/index.vue";
import Treeselect from "@riophae/vue-treeselect";
import PersonSelect from "@/components/PersonSelect/index.vue";
import {treeselect} from "@/api/system/dept";
import {getFileLoanApplication, updateFileLoanApplication} from "@/api/business/fileLoanApplication";
import {listDirectory} from "@/api/business/directory";

export default {
  name: "BorrowApplication",
  dicts: ['is_print', 'borrow_state'],
  props: {
    businessKey: String, // 业务唯一标识
    parentTaskId: String, // 父级任务id
    taskId: String // 任务id
  },
  created() {
    treeselect().then(response => {
      this.deptOptions = response.data;
    });
    this.getDicts("is_print").then(response => {
      this.whetherPrintOptions = response.data;
    });
    this.getDicts("borrow_state").then(response => {
      this.statusOptions = response.data;
    });
    this.getDocOptions();
    this.selectDocOptions = [];
  },
  components: {
    verify,Treeselect, UserSelect, PersonSelect
  },
  data() {
    return {
      // 流程实例
      processInstanceId: '',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门树选项
      deptOptions: [],
      //档案类型
      docOptions: [],
      selectDocOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 总条数
      total: 0,
      // 借阅申请表格数据
      fileLoanApplicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否打印字典
      whetherPrintOptions: [],
      // 借阅状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        borrower: null,
        borrowingUnit: null,
        borrowerFrom: null,
        fileType: null,
        whetherPrint: null,
        status: null,
        approvalStatus: null,
      },
      // processKey: "file_lending",
      // 表单参数
      form: {
        borrower: "1,2",
      },
      // 表单校验
      rules: {
        borrower: [
          {required: true, message: "借阅人不能为空", trigger: "blur"}
        ],
        borrowingUnit: [
          {required: true, message: "借阅单位不能为空", trigger: "blur"}
        ],
        borrowerFrom: [
          {required: true, message: "被借阅人不能为空", trigger: "blur"}
        ],
        startDate: [
          {required: true, message: "借阅开始时间不能为空", trigger: "blur"}
        ],
        endDate: [
          {required: true, message: "借阅结束时间不能为空", trigger: "blur"}
        ],
      },
      taskVariables: {}, //流程变量
      taskId: undefined, //任务id
      flag: true,
      // 消息提醒
      sendMessage: {},
      processKey: 'BorrowApplication'
    };
  },
  watch: {
    businessKey: {
      immediate: true, // 很重要！！！
      handler (newVal) {
        if(newVal) {
          this.getById()
        }
      }
    }
  },
  methods: {
    getDocOptions(){
      let data = {'parentId':0};
      listDirectory(data).then(response =>{
        this.docOptions = response.data;
      })
    },
    submitCallback(){
      this.$emit("closeForm")
    },
    async getById() {
      const {data} = await getFileLoanApplication(this.businessKey)
      this.form = data;
      this.form.status = '0' ;
      let fileTypeIds = data.fileType.split(',');
      for(let i = 0; i < fileTypeIds.length; i++){
        this.selectDocOptions.push(Number(fileTypeIds[i]));
      }
    },
    /** 提交按钮 */
    submitForm() {
      updateFileLoanApplication(this.form).then(response => {
        this.taskVariables = {
          entity: this.form,
          userId :1
        };
        this.sendMessage = {
          title:'借阅申请申请',
          messageContent:'单据【'+this.form.id+"】申请"
        }
      });
      this.$refs.verifyRef.visible = true
      this.$refs.verifyRef.reset()
    }
  }
};
</script>
<style scoped>

</style>
